package com.ciwong.epaper.modules.wordlist.dao;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.ciwong.epaper.modules.epaper.dao.HeadOverrideRequest;
import com.ciwong.epaper.modules.wordlist.bean.WorkBookBean;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.utils.BaseRequestUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 练习册 相关网络请求
 *
 * @author bin
 * @version ciwong v.1.0 2015/7/23 19:50
 * @since ciwong v.1.0
 */
public class WorkBookRequest {
    public static final String TAG_REQUEST_LOGIN = "TAG_REQUEST_LOGIN";

    /**
     * 获取练习册列表
     *
     * @param brandId    品牌ID
     * @param userId     用户ID
     * @param page       页码 从1开始
     * @param pageSize   每页条数
     * @param scoreRange
     * @param callBack
     */
    public static void getWorkBookListFromNet(int brandId, long userId, int page, int pageSize, String scoreRange, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, WorkBookAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, WorkBookAction.GET_WORKBOOK_LIST);
        params.put("userId", userId + "");
        params.put("brandId", brandId + "");
        params.put("page", page + "");
        params.put("pageSize", pageSize + "");
        if (scoreRange != null)
            params.put("scoreRange", scoreRange);
//        params.put("doWorkId", doWorkId);
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        callBack.failed(volleyError);
                        CWLog.e("TAT", "volleyError.toString()");
                    }
                }, new BaseRequest.ResponseCallback() {
            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                super.success(obj, resultCode, errorCode, msg);
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                } else {
                    callBack.failed(errorCode, msg);
                }
            }

            @Override
            public void error(int errorCode, String msg) {
                super.error(errorCode, msg);
                callBack.failed(errorCode, msg);
            }
        });
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(WorkBookBean.class);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());

    }


    /**
     * 提交作业答案
     *
     * @param json     答案json
     * @param callBack 回调
     */
    public static void submitWordReadWork(final String json,
                                          final BaseCallBack callBack) {

        Map<String, String> params = new HashMap<String, String>();
        params.put(com.ciwong.libs.utils.volley.HttpRequest.REQUEST_MODULES, WorkBookAction.class.getName());
        params.put(com.ciwong.libs.utils.volley.HttpRequest.REQUEST_ACTION, WorkBookAction.POST_WORDLIST_SUBMIT);
        HeadOverrideRequest request = new HeadOverrideRequest(Request.Method.POST, params,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callBack.failed(error);
                    }
                }, new BaseRequest.ResponseCallback() {

            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg) {
                if (resultCode == 0 && errorCode == 0) {

                    if (null != callBack)
                        callBack.success(obj);

                } else {

                    if (null != callBack)
                        callBack.failed(errorCode, msg);
                }
            }
        }) {
            @Override
            public byte[] getBody() throws AuthFailureError {
                return json.getBytes();
            }
        };
        request.setResponseDataType(com.ciwong.libs.utils.volley.HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(Boolean.class);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }


}
